<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="com.wxtf.platform.plugins.user.*"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
	UserBean user = (UserBean)request.getSession().getAttribute("userBean");
	String _db = user.getDbType();
	String[] ids = user.getCurSubOrgId().split(",");
	String announcementLimit = user.getGlobalMap().get("Announcement Limit");
	String noticeTime = user.getGlobalMap().get("Notice Time");

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<base href="<%=basePath%>"/>
		<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
		<meta http-equiv="pragma" content="no-cache"/>
		<meta http-equiv="cache-control" content="no-cache"/>
		<meta http-equiv="expires" content="0"/>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

		<link type="text/css" rel="stylesheet" href="ligerUI/skins/Aqua/css/ligerui-all.css" />
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/<%=user.getUserCss()%>/css/ligerui-all.css" />
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/ligerui-icons.css" />
		<link type="text/css" rel="stylesheet" href="resource/css/style.css" />

		<script type="text/javascript" src="js/jquery/jquery-1.7.2.js"></script>
		<script type="text/javascript" src="ligerUI/js/core/base.js"></script>
		<script type="text/javascript" src="ligerUI/js/ligerui.min.js"></script>
		<script type="text/javascript" src="js/common.js"></script>
		<script type="text/javascript" src="js/validata.js"></script>

		<script type="text/javascript" src="ligerUI/js/plugins/ligerGrid.js"></script>
		<script type="text/javascript" src="ligerUI/js/plugins/ligerTree.js"></script>
		<script type="text/javascript" src="ligerUI/js/plugins/ligerForm.js"></script>
		<script type="text/javascript" src="ligerUI/js/plugins/ligerComboBox.js"></script>
		<script type="text/javascript" src="ligerUI/js/plugins/ligerDateEditor.js"></script>
		
		<script type="text/javascript" src="ligerUI/jquery-validation/jquery.validate.min.js" ></script> 
		<script type="text/javascript" src="ligerUI/jquery-validation/jquery.metadata.js"></script>
		<script type="text/javascript" src="ligerUI/jquery-validation/messages_cn.js" ></script>
<script type="text/javascript">
var _db = '<%=_db%>';
var isEdit = false;
var isRep = false;
var html = "";
_jsonObj.classname = "com.wxtf.platform.main.report.QuarterlyUsageReport";
_jsonObj.methodname = "queryPage";
_jsonObj.logicdata = "[{\"year\":\"\",\"quarterly\":\"\"}]";
var _jsonTit = {};
_jsonTit.classname = "com.wxtf.platform.main.contraceptives.ContraceptiveManagerAction";
_jsonTit.methodname = "getConTitle";
_jsonTit.logicdata = "[{\"year\":\"\",\"quarterly\":\"\"}]";
var columns = [];
var gridData = [];
var value1 = [];
var value2 = [];
var year = "";
var quarterly = "";
var iscanrp = true;
var baobiao_alert_win;
$(function(){
	if(<%=ids.length%> <= 1){
		$("#editbtn").css("width","0px");
	}
	html = $("#tb").html();
	select('','');
	$("#serbtn").click(function(){
		$(this).hide();
		$("#his").show();
		$("#ref").hide();
	});
	$("#selbtn").click(function(){
		var y = $("#hisYear").val();
		var m = $("#hisQu").val();
		if(y == '' || m == ''){
			succMsgTip('请设置年和季度');
			return;
		}
		year = y;
		quarterly = m;
		$("#okrbtn").hide();
		$("#alertbtn").hide();
		select(y,m);
		$(this).attr("disabled","disabled");
		$(this).css("color","#666666");
  		doTimeOut2(5);
	});
	$(".minput").focus(function(){
  		$(this).blur();
	});
	$("#ref").click(function(){
		select(year,quarterly);
  		$(this).attr("disabled","disabled");
  		$(this).css("color","#666666");
  		doTimeOut1(5);
	});
	$("#backbtn").click(function(){
		$(".ba").show();
	});
	$("#okrbtn").click(function(){
		_jsonObj.methodname = "oneKeyReport";
		if(year != '' && quarterly != ''){
			$("#okrbtn").hide();
			$("#main").hide();
			$("#btns").hide();
			$("#message").show();
			_jsonObj.logicdata = '[{"year":"'+year+'","quarterly":"'+quarterly+'"}]';
			setTimeout(function(){
				$.ajax({
					type: "post",
					url: "/baseplatform/com/wxtf/platform/servlet/SystemControlServlet.thtf",
					data: _jsonObj,
					dataType: 'json',
					async:true,
					success: function(data,status){
								select(year,quarterly);
								succMsgTip("一键上报成功");
								$("#btns").show();
								$("#message").hide();
							},
					error: function(XMLHttpRequest, textStatus, errorThrown){
								select(year,quarterly);
								warningTip(textStatus+';'+errorThrown);
								$("#btns").show();
								$("#message").hide();
							}
				});
			},1000);
		}
	});
	$("#alertbtn").click(function(){
		var unsubmit = getUnsubmitData();
		$("#announcement_content").val("请以下单位（" + unsubmit.names + "）于<%=announcementLimit==null?28:announcementLimit%>日前及时上报" + year + "年第" + quarterly + "季度药具使用发放情况报表");
		if(baobiao_alert_win == null){
			baobiao_alert_win = initWindowByDiv('baobiao_alert', '发送通知公告', 540, 360,function(){
				$.ligerDialog.confirm("您确定要发送提醒通知和公告吗？", function (result) {
					if(result == true){
						_jsonObj.logicdata = "[{\"type\":\"" + 3 + "\",\"year\":\"" + year + "\",\"quarter\":\"" + quarterly + "\",\"ids\":\"" + unsubmit.ids + "\",\"announcement\":\"" + encodeURI(encodeURI($("#announcement_content").val())) + "\",\"atime\":\"" + $("#announcement_time").val() + "\",\"ntime\":\"<%=noticeTime==null?0:noticeTime%>\"}]";
						_jsonObj.methodname = "alert";
						callserver(_jsonObj, function(data){
							if(data.resultrecord.status == "success"){
								succMsgTip(data.resultrecord.message);
							}else{
								warningTip(data.resultrecord.message);
							}
						}, ajaxCallbackErrorFunc);
					}
					baobiao_alert_win.hide();
				});
			},function(item, dialog){
				dialog.hide();
			});
		}else{
			baobiao_alert_win.show();
		}
	});
});
function doTimeOut1(i){
	var tt = i;
	if(tt == 0){
		$("#ref").val("刷新");
		$("#ref").removeAttr("disabled");
		$("#ref").css("color","#fff");
	}else{
		$("#ref").val("刷新("+tt+"s)");
		setTimeout(function(){
			doTimeOut1(tt-1);
		},1000);
	}
}
function doTimeOut2(i){
	var tt = i;
	if(tt == 0){
		$("#selbtn").val("搜索");
		$("#selbtn").removeAttr("disabled");
		$("#selbtn").css("color","#fff");
	}else{
		$("#selbtn").val("搜索("+tt+"s)");
		setTimeout(function(){
			doTimeOut2(tt-1);
		},1000);
	}
}
function formatname(a){
	var str = '';
	var strarr = a.split("");
	var n = strarr.length;
	if(n >= 10){
		for(var i = 0; i < n; i++){
			str += strarr[i];
			if(i%2 == 1){
				str += '<br/>';
			}
		}
	}else{
		if(n > 0){
			str += strarr[0];
			for(var i = 1;i < strarr.length; i++){
				str +='<br/>'+strarr[i];
			}
		}
	}
	return str;
}
//上报 
function doReport(){
	var obj;
	var arr = [];
	var mrArray = "";
	var date = $("#tbrq_nian").val()+'-'+$("#tbrq_yue").val()+'-'+$("#tbrq_ri").val();
	var m = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/.test(date);
	if(!m){
		warningTip('填报日期格式不正确，正确的格式如:2000年01月01');
		return;
	}
	var n = gridData[0].values.length+7;
	for(var p = 1; p < gridData.length ; p++){
		if(!gridData[p].isRepQ){
			warningTip('失败:下属机构-'+gridData[p].orgname+'未上报。');
			return;
		}
	}
	if(gridData[0].tmamr_matron_num == undefined && gridData.length < 2){
		warningTip('失败:当前机构该季期末药具使用情况未上报，请先上报该季最后一月的药具月报表。');
		return;
	}
	if(isRep){
		_jsonObj.methodname = "update";
	}else{
		_jsonObj.methodname = "save";
	}
	for(var i = 7; i < n ; i++){
		var ele = "#x"+i+"y0";
		obj = {};
		obj.id = $(ele).children("input").attr("id");
		obj.kind = $(ele).children("input").attr("name");
		obj.value = $(ele).children("a").html();
		arr.push(obj);
	}
	if(<%=ids.length%> > 1){
		mrArray = $("#x1y0").children("input").attr("id")+","+$("#x1y0").children("a").html()+","+$("#x2y0").children("a").html()+","
			+$("#x3y0").children("a").html()+","+$("#x4y0").children("a").html()+","+$("#x5y0").children("a").html()+","+$("#x6y0").children("a").html()+","
			+$("#tmamr_thismonth_birth_nums").val()+","+$("#tmamr_unmarry_num").val()+","+$("#tmamr_widowed_num").val()+","
			+$("#tmamr_barrenness_num").val()+","+$("#tmamr_menopause_num").val();         
	}
	_jsonObj.logicdata = '[{"year":"'+year+'","quarterly":"'+quarterly+'","mrArray":"'+mrArray+'","repArray":'+JSON.stringify(arr)+',"reportTime":"'+$("#tbrq_nian").val()+'-'+$("#tbrq_yue").val()+'-'+$("#tbrq_ri").val()+'","arr1":'+JSON.stringify(value1)+',"arr2":'+JSON.stringify(value2)+'}]';
	value1 = [];
	value2 = [];
	callserver(_jsonObj, function(data,status){
		succMsgTip(data.resultrecord);
		$("#savebtn").hide();
		$("#x1y0").parent("tr").css("background-color","#fff");
	},function(data,status){succMsgTip(data.resultrecord);});
}
function revoke(org,mid,name){
	$.ligerDialog.confirm('确定要退回'+name+'药具使用情况上报？（删除不可恢复）', function (opt) { 			
		if(opt){
			_jsonObj.methodname = "revoke";
			_jsonObj.logicdata = "[{\"year\":\""+year+"\",\"quarterly\":\""+quarterly+"\",\"orgid\":\""+org+"\",\"mid\":\""+mid+"\"}]";
			callserver(_jsonObj, function(data,status){	
				select(year,quarterly);
				doCreate();
				succMsgTip("退回成功");
			},function(data,status){succMsgTip("操作失败，请稍后重试。");});
		}
	});
}
//查询数据
function select(y,q){
	$("#main").hide();
	$("#tb").html('');
	$("#tb").html(html);
	_jsonObj.methodname = "queryPage";
	_jsonTit.methodname = "getConTitle";
	_jsonObj.logicdata = "[{\"year\":\""+y+"\",\"quarterly\":\""+q+"\"}]";
	_jsonTit.logicdata = "[{\"year\":\""+y+"\",\"quarterly\":\""+q+"\"}]";
	callserver(_jsonTit, function(data,status){
		if(data.length > 0){
			var obj;
			var n;
			var s = 0;
			var str1 = '',str2 = '';
		  	for(var i = 0;i < data.length; i++ ){
		  		obj = data[i];
		  		n = obj.children.length;
		  		if(n > 0){
		  			s += n;
		  			str1 += '<td colspan="'+n+'" class="ttd2">'+obj.name+'</td>'; 
		  			var objs = obj.children;
			  		for(var j = 0;j < objs.length;j++){
			  			str2 +='<td class="ttd2" height="150">'+formatname(objs[j].name)+'</td>';
			  		}
		  		}else{
		  			s += 1;
		  			str1 += '<td colspan="1" rowspan="2" width="40" class="ttd2">'+formatname(obj.name)+'</td>'; 
		  		}
		  	}
		  	$("#utpn").attr("colspan",s+1);
		  	str1 += '<td rowspan="2" class="ttd2" style="width: 40px!important;">合<br/>计</td>';
		  	$("#kind").html(str1);
		  	$("#tmaTitle").html(str2);
		}
			
	},function(data,status){});
	
	callserver(_jsonObj, function(data,status){
		gridData = data.gridData;
		var str;
		year = data.year;
		quarterly = data.quarterly;
		$("#reportname").html(data.year+"年第"+data.quarterly+"季度避孕药具使用发放报表");
		var darr = data.reportTime.split("-");
		$("#tbrq_nian").val(darr[0]);
		$("#tbrq_yue").val(darr[1]);
		$("#tbrq_ri").val(darr[2]);
		$("#tmamr_thismonth_birth_nums").val(data.tmamr_thismonth_birth_nums);
		$("#tmamr_unmarry_num").val(data.tmamr_unmarry_num);
		$("#tmamr_widowed_num").val(data.tmamr_widowed_num);
		$("#tmamr_barrenness_num").val(data.tmamr_barrenness_num);
		$("#tmamr_menopause_num").val(data.tmamr_menopause_num);
		if(data.gridData.length == 1){
			isRep = gridData[0].isRepQ;
			var color = '';
			if(!isRep){
				color = 'style="background-color:#ffccff;"';
			}
			str = '<tr class="orgDataTr" '+color+'>'
				+'<td class="ttd">总计</td>';
			str	+= '<td class="ttd" id="x1y0"><input id="'+gridData[0].tmamr_id+'" name="tmamr_matron_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+(gridData[0].tmamr_matron_num == undefined?0:gridData[0].tmamr_matron_num)+'</a></td>';
			str	+= '<td class="ttd" id="x2y0"><input id="'+gridData[0].tmamr_id+'" name="tmamr_women_fertility_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+(gridData[0].tmamr_women_fertility_num == undefined?0:gridData[0].tmamr_women_fertility_num)+'</a></td>';
			str	+= '<td class="ttd" id="x3y0"><input id="'+gridData[0].tmamr_id+'" name="tmamr_ligation_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+(gridData[0].tmamr_ligation_num == undefined?0:gridData[0].tmamr_ligation_num)+'</a></td>';
			str	+= '<td class="ttd" id="x4y0"><input id="'+gridData[0].tmamr_id+'" name="tmamr_sheung_wan_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+(gridData[0].tmamr_sheung_wan_num == undefined?0:gridData[0].tmamr_sheung_wan_num)+'</a></td>';
			str	+= '<td class="ttd" id="x5y0"><input id="'+gridData[0].tmamr_id+'" name="tmamr_skinburied_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+(gridData[0].tmamr_skinburied_num == undefined?0:gridData[0].tmamr_skinburied_num)+'</a></td>';
			str	+= '<td class="ttd" id="x6y0"><input id="'+gridData[0].tmamr_id+'" name="tmamr_keep_certificate_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+(gridData[0].tmamr_keep_certificate_num == undefined?0:gridData[0].tmamr_keep_certificate_num)+'</a></td>';
			var sum = 0;
			var arr = gridData[0].values;
			var classname;
			for(var s = 0; s < arr.length; s++){
				var id = "";
				if(arr[s].quid == undefined){
					id = arr[s].tmadid;
				}else{
					id = arr[s].quid;
				}
				str += '<td class="editTd" id="x'+(7+s)+'y0"><input id="'+id+'" name="'+arr[s].kind+'" type="text" class="input2"/><a class="ma" href="javascript:void(0)">'+(arr[s].usenum==''?0:arr[s].usenum)+'</a></td>';
				sum += parseInt(arr[s].usenum == '' ? 0 : arr[s].usenum);
			}
			var len = arr.length;
			str +='<td class="ttd" id="x'+(len+7)+'y0">'+sum+'</td>'
				+'<td class="ttd" id="x'+(len+8)+'y0">'+sum+'</td>'
				+'<td class="ttd" id="x'+(len+9)+'y0">100%</td>';
				str += '</tr>';
			$("#tmaTitle").after(str);
		}else{
			isRep = gridData[0].isRepQ;
			str = '';
			var classname;
			for(var i = 0; i < gridData.length; i++){
				if(i == 0 || !gridData[i].isRepQ){
					classname = "ttd";
				}else{
					classname = "editTd";
				}
				var cname;
				if(gridData[i].isRepQ){
					cname='';
				}else{
					cname='style="background-color:#ffccff"';
				}
				str += '<tr class="orgDataTr" '+cname+'>';
				if(i == 0){
					str +='<td class="ttd1" id="x'+0+'y'+i+'">总计</td>';
					str +='<td class="ttd" id="x'+1+'y'+i+'"><input id="'+gridData[0].tmamr_id+'" type="text" class="input1"/><a class="ta" href="javascript:void(0)">'+(gridData[i].tmamr_matron_num == undefined ? 0 : gridData[i].tmamr_matron_num )+'</a></td>';
					str +='<td class="ttd" id="x'+2+'y'+i+'"><input type="text" class="input1"/><a class="ta" href="javascript:void(0)">'+(gridData[i].tmamr_women_fertility_num == undefined ? 0 : gridData[i].tmamr_women_fertility_num )+'</a></td>';
					str +='<td class="ttd" id="x'+3+'y'+i+'"><input type="text" class="input1"/><a class="ta" href="javascript:void(0)">'+(gridData[i].tmamr_ligation_num == undefined ? 0 : gridData[i].tmamr_ligation_num )+'</a></td>';
					str +='<td class="ttd" id="x'+4+'y'+i+'"><input type="text" class="input1"/><a class="ta" href="javascript:void(0)">'+(gridData[i].tmamr_sheung_wan_num == undefined ? 0 : gridData[i].tmamr_sheung_wan_num )+'</a></td>';
					str +='<td class="ttd" id="x'+5+'y'+i+'"><input type="text" class="input1"/><a class="ta" href="javascript:void(0)">'+(gridData[i].tmamr_skinburied_num == undefined ? 0 : gridData[i].tmamr_skinburied_num )+'</a></td>';
					str +='<td class="ttd" id="x'+6+'y'+i+'"><input type="text" class="input1"/><a class="ta" href="javascript:void(0)">'+(gridData[i].tmamr_keep_certificate_num == undefined ? 0 : gridData[i].tmamr_keep_certificate_num )+'</a></td>';
				}else{
					if(!isRep && gridData[i].isRepQ){
						str +='<td class="ttd1" id="x'+0+'y'+i+'"><a class="ma" href="javascript:void(0)">'+gridData[i].orgname+'</a><a class="ba" title="退回此单位上报" style="color:#000;font-size:10px;display:none;" href="javascript:revoke('+"'"+gridData[i].orgid+"','"+gridData[i].tmamr_id+"','"+gridData[i].orgname+"'"+')">  退回</a></td>';
					}else{
						str +='<td class="ttd1" id="x'+0+'y'+i+'"><a class="ta" href="javascript:void(0)">'+gridData[i].orgname+'</a></td>';
					}
					if(gridData[i].tmamr_matron_num == undefined){
						str +='<td class="ttd" id="x'+1+'y'+i+'">0</td>';
					}else{
						str +='<td class="'+classname+'" id="x'+1+'y'+i+'"><input id="'+gridData[i].tmamr_id+'" name="tmamr_matron_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+gridData[i].tmamr_matron_num+'</a></td>';
					}
					if(gridData[i].tmamr_women_fertility_num == undefined){
						str +='<td class="ttd" id="x'+2+'y'+i+'">0</td>';
					}else{
						str +='<td class="'+classname+'" id="x'+2+'y'+i+'"><input id="'+gridData[i].tmamr_id+'" name="tmamr_women_fertility_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+gridData[i].tmamr_women_fertility_num+'</a></td>';
					}
					if(gridData[i].tmamr_ligation_num == undefined){
						str +='<td class="ttd" id="x'+3+'y'+i+'">0</td>';
					}else{
						str +='<td class="'+classname+'" id="x'+3+'y'+i+'"><input id="'+gridData[i].tmamr_id+'" name="tmamr_ligation_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+gridData[i].tmamr_ligation_num+'</a></td>';
					}
					if(gridData[i].tmamr_sheung_wan_num == undefined){
						str +='<td class="ttd" id="x'+4+'y'+i+'">0</td>';
					}else{
						str +='<td class="'+classname+'" id="x'+4+'y'+i+'"><input id="'+gridData[i].tmamr_id+'" name="tmamr_sheung_wan_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+gridData[i].tmamr_sheung_wan_num+'</a></td>';
					}
					if(gridData[i].tmamr_skinburied_num == undefined){
						str +='<td class="ttd" id="x'+5+'y'+i+'">0</td>';
					}else{
						str +='<td class="'+classname+'" id="x'+5+'y'+i+'"><input id="'+gridData[i].tmamr_id+'" name="tmamr_skinburied_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+gridData[i].tmamr_skinburied_num+'</a></td>';
					}
					if(gridData[i].tmamr_keep_certificate_num == undefined){
						str +='<td class="ttd" id="x'+6+'y'+i+'">0</td>';
					}else{
						str +='<td class="'+classname+'" id="x'+6+'y'+i+'"><input id="'+gridData[i].tmamr_id+'" name="tmamr_keep_certificate_num" type="text" class="input1"/><a class="ma" href="javascript:void(0)">'+gridData[i].tmamr_keep_certificate_num+'</a></td>';
					}
				}
				var sum = 0;
				var arr = gridData[i].values;
				var temp = "";
				var len = arr.length;
				for(var s = 0; s < arr.length; s++){
					if(gridData[i].isRepQ){
						if(i == 0){
							str += '<td class="ttd" id="x'+(7+s)+'y'+i+'"><input id="'+arr[s].quid+'" name="'+arr[s].kind+'" type="text" class="input2"/><a class="ta" href="javascript:void(0)">'+arr[s].usenum+'</a></td>';
						}else{
							str += '<td class="'+classname+'" id="x'+(7+s)+'y'+i+'"><input id="'+arr[s].quid+'" name="'+arr[s].kind+'" type="text" class="input2"/><a class="ma" href="javascript:void(0)">'+arr[s].usenum+'</a></td>';
						}
					}else{
						if(arr[s].usenum == ''){
							if(i == 0){
								str += '<td class="ttd" id="x'+(7+s)+'y'+i+'"><input id="'+arr[s].tmadid+'" name="'+arr[s].kind+'" type="text" class="input2"/><a class="ta" href="javascript:void(0)">0</a></td>';
							}else{
								str += '<td class="ttd" id="x'+(7+s)+'y'+i+'">0</td>';
							}
						}
					}
					sum += parseInt(arr[s].usenum == '' ? 0 : arr[s].usenum);
				}
				str +='<td class="ttd" id="x'+(len+7)+'y'+i+'">'+sum+'</td>'
					+'<td class="ttd" id="x'+(len+8)+'y'+i+'">'+sum+'</td>'
					+'<td class="ttd" id="x'+(len+9)+'y'+i+'">100%</td>';
					str += '</tr>';
			}
			$("#tmaTitle").after(str);
			if(gridData.length > 1){
				for(var i = 1; i < 7; i++){
					calOneCol(i);
				}
			}
			if(!isRep){
				for(var i = 7; i < gridData[0].values.length+7; i++){
					calOneCol(i);
				}
				calOneRow(0);
			}
		}
		if($(".ba").length > 0){
			$("#backbtn").show();
		}else{
			$("#backbtn").hide();
		}
	}, errorFunc);	
	if(isRep){
		$("#main").show();
		$("#t1").hide();
		$("#expbtn").show();
		$("#editbtn").show();
	}else{
		$("#tm").html(year+'年第'+quarterly+'季度');
		$("#main").hide();
		$("#t1").show();
		$("#savebtn").hide();
		$("#expbtn").hide();
		$("#editbtn").hide();
	}
	/**
	$(".editTd").mouseover(function(event){
		var css = $(this).css("background-color");
		$(this).attr("bgc",css)
		$(this).css("background-color","#4fa8e6");
	});
	$(".editTd").mouseout(function(event){
		$(this).css("background-color",$(this).attr("bgc"));
	});
	*/
	$(".editTd").click(function(event){
		if(isEdit){
			if($(this).children("a").css("display")!='none'){
				var ele1 = $(this).children("a");
				var str = $(ele1).html();
				$(ele1).hide();
				var ele2 = $(this).children("input");
				$(ele2).css("width",$(ele1).css("width"));
				$(ele2).show();
				$(ele2).val(str);
				$(ele2).focus();
				$(ele2).keyup(function(){
					this.value=this.value.replace(/\D/g,'');
				});
				$(ele2).one("focusout", function(){
				  	this.value=this.value.replace(/\D/g,'');
				  	if($(ele1).html() != $(ele2).val()){
				  		pusharr($(ele2).attr("class"),ele2);
				  	}
				  	$(ele1).html($(ele2).val());
				  	$(ele2).hide();
				  	$(ele1).show();
				  	$(ele2).unbind("keyup");
			  		var s = $(ele2).parent("td").attr("id");
			  		calOneCol(s.replace("x","").split("y")[0]);
				  	if($(ele2).attr('class') == 'input2'){
				  		calOneRow(s.replace("x","").split("y")[1]);
				  		if(gridData.length > 1){
				  			calOneRow(0);
				  		}
				  	}
				});
			}
		}
	});
}
//切换编辑模式
function doEdit(){
	$("#editbtn").hide();
	$("#savebtn").show();
	$("#serbtn").hide();
	$("#expbtn").hide();
	$("#ref").hide();
	$("#his").hide();
	$("#cancelbtn").show();
	isEdit = true;
} 
/*取消*/

function cancel(){
	$("#editbtn").show();
	if(isRep){
		$("#savebtn").hide();
	}
	$("#expbtn").show();
	$("#serbtn").show();
	$("#ref").show();
	$("#cancelbtn").hide();
	//select(year,quarterly);
	isEdit = false;
	
} 
function pusharr(a,b){
	var s = $(b).attr("id");
	if(a == 'input1'){
		for(var i = 0; i < value1.length; i++){
			if(value1[i].id == s && value1[i].kind == $(b).attr("name")){
				value1[i].value = $(b).val();
				return;
			}
		}
		var o = {};
		o.id = s;
		o.value = $(b).val();
		o.kind = $(b).attr("name");
		value1.push(o);
	}else{
		for(var i = 0; i < value2.length; i++){
			if(value2[i].id == s){
				value2[i].value = $(b).val();
				return;
			}
		}
		var o = {};
		o.id = s;
		o.value = $(b).val();
		value2.push(o);
	}
}
//计算一列
function calOneCol(a){
	var orgs = gridData.length;
	if(orgs > 1){
		var sum = 0;
		var value = "";
		for(var i = 1; i < orgs; i++){
			value = $("#x"+a+"y"+i).children("a").html();
			if(value != null && value!=''){
				sum = sum+parseInt(value);
			}
		}
	$("#x"+a+"y0 a").html(sum);
	}
} 
//计算一行
function calOneRow(a){
	var cols = gridData[0].values.length+7;
	var sum = 0;
	var value = "";
	for(var i = 7; i < cols; i++){
		if($("#x"+i+"y"+a+" a").length > 0){
			value = $("#x"+i+"y"+a+" a").html();
		}else{
			value = $("#x"+i+"y"+a).html();
		}
		if(value != null && value!=''){
				sum = sum+parseInt(value);
		}
	}
	$("#x"+(cols)+"y"+a).html(sum);
	$("#x"+(cols+1)+"y"+a).html(sum);
}
//校验年控制字符输出
function ckyear(a){
	var str = a.replace(/\D/g,'');
	str = str.substring(0,4);
	return str;
} 
/*快速生成预览*/
function doCreate(){
	$("#t1").hide();
	$("#main").show();
	$("#savebtn").show();
	$("#expbtn").show();
	$("#editbtn").show();
	if('<%=user.getOrgPId(user.getCurOrgId())%>' == ''){
		//$("#okrbtn").show();
		$("#alertbtn").show();
	}
}
function getAllData(){
	var data = "[";
	var tr_size = $("tr.orgDataTr").size();
	$("tr.orgDataTr").each(function(tr_index){
		var dataRow = "{";
		var td_size = $(this).children("td").size();
		$(this).children("td").each(function(td_index){
			if(td_index == 0){
				if($(this).children(".ma").length == 1){
					dataRow = dataRow + '\"' + td_index + '\" : \"' + encodeURI(encodeURI($(this).children(".ma").text())) + '\"';
				}else{
					dataRow = dataRow + '\"' + td_index + '\" : \"' + encodeURI(encodeURI($(this).text())) + '\"';
				}
			}else{
				dataRow = dataRow + '\"' + td_index + '\" : \"' + $(this).text() + '\"';
			}
			if(td_index != td_size - 1){
				dataRow = dataRow + ",";
			}
		});
		
		dataRow = dataRow + "}";
		if(tr_index != tr_size - 1){
			dataRow = dataRow + ",";
		}
		data = data + dataRow;
	});
	data = data + "]";
	return data;
}

function getTableKind(){
	var data = "[";
	var kind_size = $("#kind td").size();
	$("#kind td").each(function(index){
		if(index != kind_size - 1){
			var dataRow = "{\"name\":\"" + encodeURI(encodeURI($(this).text())) + "\",\"colspan\":\"" + $(this).attr("colspan") + "\"}";
			if(index != kind_size - 2){
				dataRow = dataRow + ",";
			}
			data = data + dataRow;
		}
	});
	
	data = data + "]";
	return data;
}

function getTableTmaTitle(){
	var data = "[";
	var tmaTitle_size = $("#tmaTitle td").size();
	$("#tmaTitle td").each(function(index){
		var dataRow = "{\"name\":\"" + encodeURI(encodeURI($(this).text())) + "\"}";
		if(index != tmaTitle_size - 1){
			dataRow = dataRow + ",";
		}
		data = data + dataRow;
	});
	data = data + "]";
	return data;
}
//导出excel
function download(){
	$(".ba").hide();
	var kind = getTableKind();
	var tmaTitle = getTableTmaTitle();
	var orgData = getAllData();
	_jsonObj.logicdata = "[{\"year\":\"" + year + 
		"\",\"quarterly\":\"" + quarterly +
		"\",\"td_count\":\"" + $(".orgDataTr:first td").size() + 
		"\",\"tmaTitle_td_count\":\"" + $("#tmaTitle td").size() + 
		"\",\"kind\":" + kind + 
		",\"tmaTitle\":" + tmaTitle + 
		",\"tmamr_thismonth_birth_nums\":\"" + $("#tmamr_thismonth_birth_nums").val() + 
		"\",\"tmamr_unmarry_num\":\"" + $("#tmamr_unmarry_num").val() + 
		"\",\"tmamr_widowed_num\":\"" + $("#tmamr_widowed_num").val() + 
		"\",\"tmamr_barrenness_num\":\"" + $("#tmamr_barrenness_num").val() + 
		"\",\"tmamr_menopause_num\":\"" + $("#tmamr_menopause_num").val() + 
		"\",\"dwfzr\":\"" + $("#dwfzr").val() + 
		"\",\"tbr\":\"" + $("#tbr").val() + 
		"\",\"tbrq_nian\":\"" + $("#tbrq_nian").val() + 
		"\",\"tbrq_yue\":\"" + $("#tbrq_yue").val() + 
		"\",\"tbrq_ri\":\"" + $("#tbrq_ri").val() + 
		"\",\"data\":" + orgData + "}]";
		
	_jsonObj.methodname = "download";
	
	callserver(_jsonObj, function(data, textStatus){
		
		if(data.result){
			downloadFile(data.resultrecord);
		} else {
			succMsgTip(data.resultrecord);
		}
		
	}, function(data, textStatus){
	});
}
/**
获取未上报单位的数据
*/
function getUnsubmitData(){
	if(gridData == []) return "";
	var ids = "";
	var names = "";
	for(var i = 1; i < gridData.length; i++){
		if(!gridData[i].isRepQ){
			ids += gridData[i].orgid+",";
			names += gridData[i].orgname+"，";
		}
	}
	ids=ids.substring(0,ids.length-1); 
	names=names.substring(0,names.length-1); 
	return {"ids" : ids, "names" : names};
}
/*
 * ajax失败回调
 */
function ajaxCallbackErrorFunc(data, textStatus){
	warningTip(data.resultrecord.message);
}
</script>
<style>
	.ttd{
		border: 1px solid #CCCCCC;
		text-align: center;
		width: auto!important; 
		height: 30px;
		min-width: 40px;
		font-size: 14px;
	}
	.ttd2{
		border: 1px solid #CCCCCC;
		text-align: center;
		width: auto!important; 
		height: 30px;
		min-width: 40px;
		font-size: 14px;
		background-color: #EEEEEE;
	}
	.ttd1{
		border: 1px solid #CCCCCC;
		text-align: center;
		height: 30px;
		font-size: 14px;
		white-space:nowrap;
	}
	.editTd{
		border: 1px solid #CCCCCC;
		text-align: center;
		width: 40px!important;
		height: 30px;
		min-width: 40px;
	}
	.redTD{
		border: 1px solid #CCCCCC;
		text-align: center;
		width: 40px!important;
		height: 30px;
		min-width: 40px;
		background-color: #ffccff;
	}
	.ma{
		text-decoration: none;
	}
	.ba{
		text-decoration: none;
	}
	.ta{
		text-decoration: none;
		cursor: default;
		outline-style: none;
		
	}
	.input1{
		border: 1px solid #4fa8e6;
		text-align: center;
		height: 20px;
		display: none;
		min-width: 30px;
	}
	.input2{
		border: 1px solid #4fa8e6;
		text-align: center;
		height: 20px;
		display: none;
		min-width: 30px;
	}
	.minput{
		border:0;
		width: 60px;
		border-bottom: 1px solid #000;
		text-align: center; 
	}
	.page-win{
		display:none;
		z-index:10;
		position:absolute;
		top:10px;
		left:10px;
		right:10px;
		bottom:10px;
		border:1px solid #fff;
	    background-image:url(../images/gn_bg1.png);
	    border-radius:10px 10px 10px 10px;	
	}
</style>
	<body style="overflow: auto;background-color: #fff;font-size: 14px;font-family: '黑体';">
		<div id="btns">
			<input class="btn2" id="editbtn" value="编辑" type="button" onclick="doEdit();" style="top: 10px;left: 10px;display: none;"/>
			<input class="btn2" id="savebtn" value="提交" type="button" onclick="doReport();" style="top: 10px;left: 10px;display: none;"/>
			<input class="btn2" id="cancelbtn" value="返回" type="button" onclick="cancel()" style="top: 10px;left: 10px;display: none;"/>
			<input class="btn2" id="expbtn" value="导出" type="button" onclick="download()" style="top: 10px;left: 10px;display: none;"/>
			<input class="btn2" id="serbtn" value="查询|新建" type="button" style="top: 10px;left: 10px;"/>
			<input class="btn2" id="backbtn" value="退回" type="button" style="top: 10px;left: 10px;display: none;"/>
			<span id="his" style="top: 10px;left: 10px;display: none;">
				<input id="hisYear" type="text" value="" onkeyup="this.value=ckyear(this.value);" style="width: 40px;height: 18px;"/>年
				<select id="hisQu" name="hisqu" style=" width: 80px;height: 20px;">
					<option value="一">第一季度</option>
					<option value="二">第二季度</option>
					<option value="三">第三季度</option>
					<option value="四">第四季度</option>
				</select>
				<input class="btn2" id="selbtn" value="搜索" type="button" style="top: 10px;left: 10px;"/>
			</span>
			<input class="btn2" id="okrbtn" value="一键上报" type="button" style="top: 10px;left: 10px;display: none;"/>
			<input class="btn2" id="alertbtn" value="提醒上报" type="button" style="top: 10px;left: 10px;display: none;"/>
			<input class="btn2" id="ref" value="刷新" type="button" style="top: 10px;left: 10px;"/>
		</div>
		<div id="t1" style="padding-left: 50px;height: 40px;font-size: 20px;text-align: justify;margin-top: 70px;display: none;">
			<p id="tm"></p>
			<br/>该季未生成报表，您可以点击
			<input class="btn2" value="生成" onclick="doCreate()" type="button" style="font-size: 15px;"/>生成该季报表预览,
			<br/>也可以通过上方的“查询|新建”按钮查看或生成指定季度的报表预览。
			<br/>生成预览后可以通过“编辑”按钮切换到编辑模式。
			<br/>说明：只有切换到编辑模式才可以修改报表内容。
			<br/>提醒：请不要忘了点击“提交”按钮将数据提交。
		</div>
		<div id="main">
			<div id="reportname" style="height: 40px;font-size: 30px;text-align: center;margin-top: 10px;">
			</div>
			<div style="height: 20px;font-size: 20px;text-align: left;margin-top: 5px;">填报单位:<%=user.getCurOrgName()%>
			</div>
			<div style="margin-top: 10px;">
				<table id="tb" cellpadding="0" cellspacing="0"  style="border: 2px solid #CCCCCC;table-layout:auto;">
					<tr>
					  <td rowspan="3" height="130" class="ttd2"></td>
					  <td rowspan="3" class="ttd2">已<br/>婚<br/>育<br/>龄<br/>妇<br/>女<br/>数</td>
					  <td rowspan="3" class="ttd2">已婚<br/>有生<br/>育条<br/>件的<br/>育龄<br/>妇女<br/>数</td>
					  <td rowspan="3" class="ttd2">已<br/>结<br/>扎<br/>人<br/>数</td>
					  <td rowspan="3" class="ttd2">已<br/>上<br/>环<br/>人<br/>数</td>
					  <td rowspan="3" class="ttd2">皮<br/>埋<br/>人<br/>数</td>
					  <td rowspan="3" class="ttd2">已<br/>孕<br/>等<br/>孕<br/>人<br/>数</td>
					  <td id="utpn" height="30" class="ttd2" style="height: 30px">使用药具人数</td>
					  <td rowspan="3" class="ttd2" style="width: 40px!important;">用<br/>药<br/>具<br/>有<br/>效<br/>人<br/>数</td>
					  <td rowspan="3" class="ttd2" style="width: 40px!important;">用<br/>药<br/>具<br/>有<br/>效<br/>率</td>
				 	</tr>
					<tr id="kind">
				 	</tr>
					<tr id="tmaTitle">
				 	</tr>
				</table>
			</div>
			<div style="font-size: 15px;text-align: left;margin-top: 5px;margin-left: 20px;">注:本季最后一月出生人数
				<input type="text" id="tmamr_thismonth_birth_nums" readonly="readonly" class="minput" value="0"/>人；
				离婚
				<input type="text" id="tmamr_unmarry_num" readonly="readonly" class="minput"  value="0"/>人；
				丧偶
				<input type="text" id="tmamr_widowed_num" readonly="readonly" class="minput"  value="0"/>人；
				不孕症
				<input type="text" id="tmamr_barrenness_num" readonly="readonly" class="minput"  value="0"/>人；
				绝经
				<input type="text" id="tmamr_menopause_num" readonly="readonly" class="minput"  value="0"/>人；
			</div>
			<div style="font-size: 15px;text-align: left;margin-top: 5px;margin-left: 20px;" >单位负责人:
				<input type="text" id="dwfzr" style="border:0;width: 150px;text-align: left;" value=""/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				填表人:
				<input type="text" id="tbr" style="border:0;width: 150px;text-align: left;" value="<%=user.getUserrelName()%>" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				填表日期:
				<input type="text" id="tbrq_nian" readonly="readonly" style="border:0;width: 60px;" class="minput" value=""/>年
				<input type="text" id="tbrq_yue" style="border:0;width: 30px;text-align: center;" value=""/>月
				<input type="text" id="tbrq_ri" style="border:0;width: 30px;text-align: center;" value=""/>日
			</div>
		</div>
		<div id="baobiao_alert" style="display: none;">
			<form id="baobiao_alert_form" name="baobiao_alert_form">
				公告内容：<br/>
				<textarea id="announcement_content" style="width:510px;height:230px;font-size:16px;line-height:22px;margin-bottom:10px;"></textarea>
				公告持续时间：<select id="announcement_time"><option value="24">一天</option><option value="48">二天</option><option value="72" selected="selected">三天</option><option value="96">四天</option><option value="120">五天</option><option value="144">六天</option><option value="168">七天</option></select>
			</form>
		</div>
		<div id="message" style="display: none;">数据量较大，请耐心等待，未完成上报之前，请不要做其他操作。</div>
	</body>
</html>
